<template><!--必须有template和div标签-->
  <div class="app-container"><!-- 调用定义的接口方法，得到接口返回的数据-->
    课程列表
        <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="courseQuery.title" placeholder="课程名称"/>
      </el-form-item>

      <!-- <el-form-item>
        <el-select v-model="courseQuery.status" clearable placeholder="课程状态">
          <el-option :value="Noraml" label="已发布"/>
          <el-option :value="Draft" label="未发布"/>
        </el-select>
      </el-form-item> -->

      <!-- 所属分类 TODO -->
        <!-- 课程分类 -->
        <el-form-item label="课程分类">
        <el-select
            v-model="courseQuery.oneSubject"
            placeholder="一级分类" @change="subjectLevelOneChanged">
            <el-option
            v-for="subjectOne in subjectOneList"
            :key="subjectOne.id"
            :label="subjectOne.title"
            :value="subjectOne.id"/>
        </el-select>

        <!-- 二级分类 -->
        <el-select v-model="courseQuery.twoSubject" placeholder="二级分类">
            <el-option
                v-for="subjectTwo in subjectTwoList"
                :key="subjectTwo.id"
                :label="subjectTwo.title"
                :value="subjectTwo.id"/>
        </el-select>
        </el-form-item>

        <!-- 课程讲师 TODO -->
        <!-- 课程讲师 -->
        <el-form-item label="课程讲师">
        <el-select
            v-model="courseQuery.teacher"
            placeholder="请选择讲师">
            <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"/>
        </el-select>
        </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="courseQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="courseQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
       </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>


        <!-- 表格 -->
    <el-table
      
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="课程名称"  />
     
      <!-- <el-table-column prop="teacher" label="讲师" width="100"/> -->
      <!-- <el-table-column label="讲师" width="100">
        <template slot-scope="scope">
          {{ scope.row.teacherName }}
        </template>
      </el-table-column> -->
      <el-table-column prop="lessonNum" label="课时数" />

       <el-table-column prop="price" label="价格" />

      <!-- <el-table-column prop="intro" label="介绍" /> -->

       <el-table-column label="课程状态" >
        <template slot-scope="scope">
          {{ scope.row.status==='Normal'?'已发布':'未发布' }}
        </template>
      </el-table-column>

      <el-table-column prop="viewCount" label="浏览数量"  />

      <el-table-column prop="gmtCreate" label="添加时间" />s

      <el-table-column label="操作" width="510" align="center">
        <template slot-scope="scope">
          <router-link :to="'/course/info/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑课程基本信息</el-button>
          </router-link>
          <router-link :to="'/course/chapter/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑课程大纲信息</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除课程的信息</el-button>
        </template>
      </el-table-column>
    </el-table>

       <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>

<script>

import course from '@/api/edu/course'
import subject from '@/api/edu/subject'
import teacher from '@/api/edu/teacher'

export default {

  data(){//定义变量和初始值
    return{//变量的名字可以随便起
      list : null,//查询之后接口返回集合
      page:1,//当前页
      limit:10,//每页显示的记录数
      total:0,//总记录数
      courseQuery:{},//查询条件
      teacherList:[],//用于封装所有的讲师的数据
      subjectOneList:[],//用于封装所有的一级分类
      subjectTwoList:[]//用于封装二级分类
    }
  },
  created(){
   this.getList()
   this.getSubjectOneList()
   this.getListTeacher()
  },
  methods:{

    //删除课程
    removeDataById(id){
       this.$confirm('此操作将永久删除该课程，以及该课程下的章节和视频，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(()=>{
      course.removeCourse(id)
      .then(()=>{
        this.$message({
          type:'success',
          message:'删除成功'
        })
        this.getList()
      })
    }).catch(response=>{
      if (response === 'cancel') {
            this.$message({
                type: 'info',
                message: '已取消删除'
            })
        }
    })
    },
    getList(page=1){
      this.page=page
      course.getListPage(this.page,this.limit,this.courseQuery)
       .then(response=>{//请求成功
        this.list=response.data.records
        this.total=response.data.total
       })
      
    },
    resetData(){//清空
      //表单输入项目清空
      this.courseQuery={}

      this.getList();
    },
     //点击某个一级分类会触发change事件，显示对应的二级分类
       subjectLevelOneChanged(value){
           //value就是一级分类的id值 框架中会自动做到把一级分类的ID传过来
           //alert(value)
           //遍历所有的分类
           for(let i=0;i<this.subjectOneList.length;i++){
               //每个一级分类
               var oneSubject =this.subjectOneList[i]
               //判断：所有一级分类的id和点击的一级分类id是否一致
               if(value===oneSubject.id){
                   //从一级分类中获取所有的二级分类
                   this.subjectTwoList=oneSubject.children
                   //把二级分类的id值清空
                   this.courseInfo.subjectId=''
               }
           }
       },
       //查询所有一级分类
       getSubjectOneList(){
           subject.getSubjectList()
           .then(response=>{
              this.subjectOneList=response.data.list
           })
       },

       //查询所有讲师
        getListTeacher(){
            course.getListTeacher()
            .then(response=>{
                this.teacherList= response.data.items
            })
        },
    
  }

}
</script>